package com.china08.yxyapi.api.comm.sws;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.service.res.statistics.impl.ResourceStatisticsEsImpl;
import com.china08.yxyapi.util.DateUtils;

/*
 * 资源学校
 */

@CrossOrigin
@RestController
public class ResourceSchoolStatisticsDataCrtController {

    private static final Logger LOGGER = Logger.getLogger(ResourceSchoolStatisticsDataCrtController.class);

    @Autowired
    ResourceStatisticsEsImpl resourceStatisticsEsImpl;

    @RequestMapping(value = "/api_yw/comm/sws/resource/school/crt/{begin}/{end}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "begin") Integer begin, @PathVariable(value = "end") Integer end) throws ParseException {

        long dayCount = DateUtils.difference(DateUtils.formatStrDate4(begin), DateUtils.formatStrDate4(end));
        Long count = new Long(dayCount);
        LOGGER.info("资源学校跑数据接收请求");

        for (int i = count.intValue(); i > 0; i--) {
            Integer context = DateUtils.formatDate4(DateUtils.subtractDay(DateUtils.formatStrDate4(end), i));
            LOGGER.info(context);

            Date aDate = DateUtils.formatStrDate4(context);
            String startDate = DateUtils.formatDate8(DateUtils.getFirstDay(aDate));
            String endDate = DateUtils.formatDate8(DateUtils.getTimesnight(aDate));
            resourceStatisticsEsImpl.ywStatisticsResourceSchoolNewEs(startDate, endDate, context);

        }

    }

}
